<?php

namespace common\components;

use common\Tools;
use GuzzleHttp\Client;
use yii\base\Component;

class ApiTool extends Component
{
    public $baseUri;

    public function generateUrl($uri, $queryParams = [])
    {
        $querys = "";
        if ($queryParams) {
            $querys = Tools::toQuery($queryParams);
        }
        $url = $this->baseUri.$uri."?".$querys;
        return $url;
    }

    public function request($uri = "", $options = [], $method = "post")
    {
        $client = new Client(['base_uri' => $this->baseUri]);
        $resp = $client->request($method, $uri, $options);
        $resp_cont = $resp->getBody()->getContents();
        $resp_cont_arr = Tools::isJson($resp_cont);
        return $resp_cont_arr;
    }
}